package com.example.mywanandroid.ui.bottom.system;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.mywanandroid.R;
import com.example.mywanandroid.base.BaseFragment;
import com.example.mywanandroid.bean.responsebean.WeChatBean;
import com.example.mywanandroid.config.Constants;
import com.example.mywanandroid.databinding.FragmentListBinding;
import com.example.mywanandroid.navinterface.ScrollToTop;
import com.example.mywanandroid.ui.adapter.SystemAdapter;
import com.google.gson.Gson;

import java.io.Serializable;
import java.util.List;

public class SystemFragment extends BaseFragment<FragmentListBinding, SystemViewModel> implements ScrollToTop {
    private static final String TAG = "SystemFragment";
    private SystemAdapter adapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_list;
    }

    @Override
    protected void initViewModel() {
        mViewModel = new ViewModelProvider(this).get(SystemViewModel.class);
    }

    @Override
    protected void bindViewModel() {

    }

    @Override
    protected void init() {
        mViewModel.loadData();
        initRefreshLayout();
        initRecycleView();
        addObserver();
    }

    private void initRefreshLayout() {
        mDataBinding.refreshLayout.setEnableLoadMore(false);
        mDataBinding.refreshLayout.setOnRefreshListener(refreshLayout -> mViewModel.refreshData());
    }

    private void initRecycleView() {
        adapter = new SystemAdapter(mViewModel.getSystemList().getValue()) {
            @Override
            protected void addListener(View view, WeChatBean bean, int position) {
                view.findViewById(R.id.card_view).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Bundle bundle = new Bundle();
                        bundle.putInt(Constants.ParamCode.PARAM2, 0);
                        bundle.putSerializable(Constants.ParamCode.PARAM1, (Serializable) bean.getChildren());
                        NavHostFragment.findNavController(SystemFragment.this).navigate(R.id.systemChildrenFragment, bundle);
                    }
                });
            }

            @Override
            protected void addChildListener(View view, WeChatBean bean, List<WeChatBean> parentList, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt(Constants.ParamCode.PARAM2, position);
                bundle.putSerializable(Constants.ParamCode.PARAM1, (Serializable) parentList);
                NavHostFragment.findNavController(SystemFragment.this).navigate(R.id.systemChildrenFragment, bundle);
            }
        };
        mDataBinding.recycle.setLayoutManager(new LinearLayoutManager(getContext()));
        mDataBinding.recycle.setAdapter(adapter);
    }

    @Override
    protected boolean isSupportLoad() {
        return true;
    }

    private void addObserver() {
        mViewModel.getSystemList().observe(this, bean -> {
            Log.d(TAG, "addObserver: " + new Gson().toJson(bean));
            mDataBinding.refreshLayout.finishRefresh();
            adapter.seetData(bean);
        });
    }

    @Override
    public void scrollToTop() {
        mDataBinding.recycle.smoothScrollToPosition(0);
    }
}
